// pages/manufacturer-detail/manufacturer-detail.js
import areaList from "../../utils/area-list";
import { Factory } from "../../models/factory";
import { Article } from "../../models/article";
import { UserInfo } from "../../models/userinfo";
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
import { config } from "../../config/config";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    detailTab: 1,
    id: "",
    info: null,
    productList: [],
    area: "",
    areaList,
    show: false,
    pageIndex: 1,
    minfo: null,
    config
  },
  tab1() {
    this.setData({
      detailTab: 1
    });
  },
  tab2() {
    if (
      !this.data.userInfo ||
      this.data.userInfo.PopedomId != "1290559635618533376"
    ) {
      return Toast("会员可见");
    }
    this.setData({
      detailTab: 2
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let userInfo = new UserInfo();
    let detail = JSON.parse(options.item);
    // detail.Content = '';
    // detail.Title='';
    this.setData({
      userInfo: userInfo.getUserInfo(),
      minfo: detail
    });
    console.log("minfo");
    console.log(this.data.minfo);
    if (this.data.minfo.PriceaAndMc) {
      this.setData({
        priceaAndMcArr: this.handlePriceaAndMc(this.data.minfo.PriceaAndMc)
      });
    }
    wx.setNavigationBarTitle({
      title: "厂家详情"
    });
    await this.init(options.id);
    this.getData();
  },
  imgLoad(e) {
    console.log("imgLoad");
    console.log(e.detail);
    this.setData({
      imgHeight: (e.detail.height / e.detail.width) * 750 + "rpx"
    });
  },

  handlePriceaAndMc(str) {
    return str.split("|").map(item => {
      return item.split(":");
    });
  },
  // 根据userId查工厂Id
  async getFactoryIdByUserId(userId) {
    let postData = {
      Search: { all: true, userId: userId, keyword: null },
      PageIndex: 1,
      PageRows: 10000,
      SortField: "id",
      SortType: "asc"
    };
    const data = await Factory.GetDataList(postData);
    if (data.Data.length > 0) {
      return data.Data[0].Id;
    }
  },

  async goDetail(e) {
    // console.log(e.currentTarget.dataset.item);
    if (this.canNotSee(e.currentTarget.dataset.aid)) {
      return;
    }
    let item = JSON.stringify(e.currentTarget.dataset.item);
    // wx.navigateTo({
    //   url: `/pages/new-product-detail/new-product-detail?id=${e.currentTarget.dataset.id}&item=${item}`
    // });
    let factoryId = await this.getFactoryIdByUserId(
      e.currentTarget.dataset.item.User_id
    );
    wx.navigateTo({
      url: `/pages/manufacturer-detail/manufacturer-detail?id=${factoryId}&item=${item}`
    });
  },
  apply() {
    // this.show = true;
    this.setData({
      show: true
    });
  },
  close() {
    this.setData({
      show: false
    });
  },
  canSee(Articlelassify_id) {
    if (
      this.data.userInfo.PopedomId != "1290559635618533376" &&
      Articlelassify_id == 2
    ) {
      return false;
    } else {
      return true;
    }
  },
  canNotSee(Articlelassify_id) {
    if (!this.data.userInfo && Articlelassify_id == 2) {
      return true;
    }
    if (
      this.data.userInfo &&
      this.data.userInfo.PopedomId != "1290559635618533376" &&
      Articlelassify_id == 2
    ) {
      return true;
    } else {
      return false;
    }
  },
  toggleDetail(id) {
    this.detailTab = id;
  },
  async init(id) {
    console.log("ceshi_id ", id);
    const data = await Factory.GetTheData({ id });
    // this.data.info = data.Data;
    // this.data.status = this.info.Status;
    console.log("ceshi ", data)
    if(!data.Data){
      return;
    }
    data.Data.img = data.Data.Imags.split(",")[0];
    console.log(data.Data.img);
    this.setData({
      info: data.Data,
      status: data.Data.Status
    });

    let code = data.Data.Title.split(",");
    this.findAreaByCode(code);
  },
  findAreaByCode(codes) {
    let area = [];
    if (codes.length > 0) {
      for (let key in this.data.areaList.province_list) {
        if (key == codes[0]) {
          area.push(areaList.province_list[key]);
        }
      }
    }
    if (codes.length == 2) {
      for (let key in this.data.areaList.city_list) {
        if (key == codes[1]) {
          area.push(areaList.city_list[key]);
        }
      }
    }
    // this.area = area.join("/");
    let addr = `${area.join("/")}/${this.data.info.Location}`;
    this.setData({
      // area: area.join("/"),
      addr
    });
  },

  async getData() {
    let data = await this.init2();
    let productList = this.data.productList.concat(data);
    console.log(productList);
    this.setData({
      productList
    });
  },
  async init2() {
    let postData = {
      Search: {
        all: true,
        Condition: "User_id",
        keyword: this.data.info.User_id
      },
      PageIndex: this.data.pageIndex,
      PageRows: 10,
      SortField: "id",
      SortType: "desc"
    };
    const data = await Article.GetDataList(postData);

    data.Data.map(item => {
      item.src = item.Imags.split(",")[0];
      item.srcs = item.Imags.split(",");
      item.time = item.CreateTime.split(" ")[0];
      return item;
    });

    return data.Data;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex++;
    this.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
